﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;

namespace EpiMapit
{
    class ResourcesPreProcessor
    {
        internal static void PreProcess(Dictionary<string, Position> byGEOCode, Dictionary<string, Position> byGEOCodeNoEndZeros,
                                        Dictionary<string, Position> byRegion, Dictionary<string, Position> byCountry,
                                        Dictionary<string, Position> byCountryISO2)
        {
            PreProcessAdminCentroid(byGEOCode, byGEOCodeNoEndZeros, byRegion);
            PreProcessWorldCountriesCentroid(byCountry, byCountryISO2);
        }

        private static void PreProcessWorldCountriesCentroid(Dictionary<string, Position> byCountry, Dictionary<string, Position> byCountryISO2)
        {
            Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("EpiMapit.Resources.world_countries_centroid.csv");

            CSVReader csv = new CSVReader(fileStream);

            // index of the country, country iso2 code, latitude and longitude
            int index_country = 0;
            int index_iso2 = 3;
            int index_latitude = 6;
            int index_longitude = 5;

            string[] fields;
            string country;
            string iso2;
            string str_latitude;
            string str_longitude;
            float latitude;
            float longitude;

            // first read titles
            fields = csv.GetCSVLine();

            // read other lines
            fields = csv.GetCSVLine();

            while (fields != null)
            {
                country = fields[index_country];
                iso2 = fields[index_iso2];
                str_latitude = fields[index_latitude];
                str_longitude = fields[index_longitude];
                latitude = float.Parse(str_latitude);
                longitude = float.Parse(str_longitude);

                Position pos = new Position(latitude, longitude);

                // may be repeated rows, they are the same location
                if (!byCountry.ContainsKey(country))
                {
                    byCountry.Add(country, pos);    
                }

                // iso2 code may be null
                if (!string.IsNullOrEmpty(iso2) && !byCountryISO2.ContainsKey(iso2))
                {
                    byCountryISO2.Add(iso2, pos);    
                }

                fields = csv.GetCSVLine();
            }

            // free them all!
            csv.Dispose();
            fileStream.Close();
        }

        private static void PreProcessAdminCentroid(Dictionary<string, Position> byGEOCode, Dictionary<string, Position> byGEOCodeNoEndZeros, Dictionary<string, Position> byRegion)
        {
            Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("EpiMapit.Resources.admin_centroid.csv");

            CSVReader csv = new CSVReader(fileStream);

            // index of the label id, city, latitude and longitude
            int index_labelId = 3;
            int index_city = 2;
            int index_latitude = 8;
            int index_longitude = 7;

            string[] fields;
            string labelId;
            string city;
            string str_latitude;
            string str_longitude;
            float latitude;
            float longitude;

            // first read titles
            fields = csv.GetCSVLine();

            // read other lines
            fields = csv.GetCSVLine();

            while (fields != null)
            {
                labelId = fields[index_labelId];
                str_latitude = fields[index_latitude];
                str_longitude = fields[index_longitude];
                latitude = float.Parse(str_latitude);
                longitude = float.Parse(str_longitude);
                city = fields[index_city];

                Position pos = new Position(latitude, longitude);

                // may be repeated rows, they are the same location
                if (!byGEOCode.ContainsKey(labelId))
                {
                    byGEOCode.Add(labelId, pos);    
                }

                // For now we are going to ignore city names like "centre", "west" etc.
                // because they are not present in our data source
                if (!byRegion.ContainsKey(city))
                {
                    byRegion.Add(city, pos);    
                }

                // cut the ending zeros in the geocode
                string GEOCodeCut = Auxiliars.CutEndingZeros(labelId);
                
                // may be repeated rows, they are the same location
                if (!byGEOCodeNoEndZeros.ContainsKey(GEOCodeCut))
                {
                    byGEOCodeNoEndZeros.Add(GEOCodeCut, pos);
                }

                fields = csv.GetCSVLine();
            }

            // free them all!
            csv.Dispose();
            fileStream.Close();
        }
    }
}
